//
//  WBNavigationController.swift
//  SinaWeibo
//
//  Created by Jiang on 2016/11/22.
//  Copyright © 2016年 Jiang. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的 NavigationBar
        navigationBar.isHidden = true
    }
    
    
    // viewController  是被 push 的控制器，设置他的左侧的按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
       
        //如果不是栈底控制器才需要隐藏，根控制器不需要处理
        // 隐藏底部的 TabBar
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
       
        
        if let vc = viewController as? WBBaseViewController{
            //取出自定义的 navItem 
            var  title = "返回"
            // 判断控制器 的级数
            
            if childViewControllers.count ==  1{
                title = childViewControllers.first?.title ?? "返回"
            }
            
            vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBackButton: true)
        
        }
        }
        super.pushViewController(viewController, animated: true)
    }
    
    // POP 返回到上一级控制器
    @objc private func popToParent() {
        popViewController(animated: true)
        
    }

}
